$(document).ready(function () {
	populateSeries();
	
	$('#search-form').submit(function(event) {
		preformSearch();
		
		event.preventDefault();
		//alert('Handler for .submit() called.');
		
		return false;
	});
	
        /*
        var timer;
        $('#search').keyup(function(){
            timer && clearTimeout(timer);
            console.log('timer start');
            timer = setTimeout(instantSearch, 500);
        });	
	*/
	$("#result").tabs();  
	$('#result').hide();
});

function instantSearch(){
    console.log('trigger');
    preformSearch();  
}


function preformSearch(){   
    $('#result').show('fast');
    firstQBLoad = true;
    firstStudyLoad = true;
    searchQB($("#search").val(),0,10,0);    
    searchStudy($("#search").val(),0,10,0);   
}


/* Load series from eXist and populate the selection box*/
function populateSeries(){
    $.ajax({
        url: backend+'/db/ddi/ws/series.xql?callback=?',
        dataType: "jsonp",
        success: function(data){
    		$.each(data.series, function(key, val) {
    			$('#series').append($("<option></option>").
    			          attr("value",val.id).
    			          text(val.name)); 
    		});
			$("#series").multiselect(
    				{selectedText: "# of # selected",noneSelectedText: 'Select series'}
    		).multiselectfilter();
    	}
    });
}